<template>
  <div>
    <div class="back">
      <router-link to="/target">
        <img :src="backSrc" style="width: 40px; height: 40px" alt="" />
      </router-link>
      <div class="text">
        添加成绩
        </div>
    </div>
    <div class="input">
      <van-cell-group>
        <van-field error required placeholder="请输入考试名称" v-model="score.active" label="考试名称" />
        <van-field error required placeholder="请输入成绩" v-model="score.yuwen" type="number" label="语文" />
        <van-field error required placeholder="请输入成绩" v-model="score.shuxue" type="number" label="数学" />
        <van-field error required placeholder="请输入成绩" v-model="score.yingyu" type="number" label="英语" />
        <van-field error required placeholder="请输入成绩" v-model="score.wuli" type="number" label="物理" />
        <van-field error required placeholder="请输入成绩" v-model="score.huaxue" type="number" label="化学" />
        <van-field error required placeholder="请输入成绩" v-model="score.shengwu" type="number" label="生物" />
        <van-field error required placeholder="请输入成绩" v-model="score.dili" type="number" label="地理" />
        <van-field error required placeholder="请输入成绩" v-model="score.lishi" type="number" label="历史" />
        <van-field error required placeholder="请输入成绩" v-model="score.zhengzhi" type="number" label="政治" />
      </van-cell-group>
    </div>
    <div class="sumbit">
      <van-button round type="primary" @click="AddScore">提交成绩</van-button>
    </div>
  </div>
</template>
<script>
import axios from "axios"
export default {
  data() {
    return {
      backSrc: require("../assets/images/back.png"),
      score: {
        active: "",
        yuwen: "",
        shuxue: "",
        yingyu: "",
        wuli: "",
        huaxue: "",
        shengwu: "",
        lishi: "",
        dili: "",
        zhengzhi: "",
      },
    };
  },
  methods: {
    AddScore() {
      this.$toast.loading("提交中");
      let active = this.score.active;
      let yuwen = (this.score.yuwen == "")?0:this.score.yuwen;
      let shuxue = (this.score.shuxue == "") ? 0:this.score.shuxue;
      let yingyu = (this.score.yingyu == "") ? 0:this.score.yingyu;
      let wuli = (this.score.wuli == "")?0:this.score.wuli;
      let huaxue = (this.score.huaxue == "")?0:this.score.huaxue;
      let shengwu = (this.score.shengwu =="")?0:this.score.shengwu;
      let lishi = (this.score.lishi == "")?0:this.score.lishi;
      let dili = (this.score.dili == "")?0:this.score.dili;
      let zhengzhi = (this.score.zhengzhi =="")?0:this.score.zhengzhi;
      let userName = this.$store.getters.getUser.name;
      // console.log(active);
      let data = {
        active,
        yuwen,
        shuxue,
        yingyu,
        wuli,
        huaxue,
        shengwu,
        lishi,
        dili,
        zhengzhi,
        userName,
      };
      console.log(data);
      if(data != undefined){
      axios
        .post("http://127.0.0.1/list/addscore", data, {
          headers: "application/x-www-form-urlencoded",
        })
        .then(res=>{
          console.log(res);
          let {data} =res;
          if(data.code == 200){
            this.$toast.success("提交成功");
          this.$router.push('/target')
          }
          else{
            this.$toast.fail("提交失败")
          }
        }).catch(err=>{
          console.log(err);
        });}
    },
  },
};
</script>
<style lang="less" scoped>
.back {
      width: 100%;
      height: 60px;
      position: fixed;
      top: 0;
      right: 0;
      background: linear-gradient(to left, #29eac4, #4284db);
      img {
        position: absolute;
        left: 0;
        top: 10px;
      }
      .text {
    text-align: center;
    line-height: 60px;
    height: 60px;
    box-sizing: border-box;
    border-bottom: 1px solid #ccc;
    font-weight: bold;
  }
}
.input{
  padding-top: 80px;
  width: 80%;
  margin: 0 auto;
}
</style>
<style lang="less">
.sumbit{
  .van-button{
  width: 80%;
    margin-top: 20px;
}
}

</style>